using System;
using System.Collections;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.UI;

namespace Minesweeper.GUI.HUD
{
    [RequireComponent(typeof(RawImage))]
    public class Minimap : MonoBehaviour
    {
        public static Minimap Instance;
        
        [NonSerialized] public Texture2D Map;
        private RawImage Image => GetComponent<RawImage>();

        private void Awake()
        {
            Instance = this;
            Map = new Texture2D(GameGenerator.Instance.width * 2, GameGenerator.Instance.height * 2,
                TextureFormat.RGBA32, false)
            {
                filterMode = FilterMode.Point
            };
            var e = new Color32[Map.width * Map.height];
            Array.Fill(e, new Color32(0xC0, 0xC0, 0xC0, 0xFF));
            Map.SetPixels32(e, 0);
            Image.texture = Map;
            ((RectTransform)transform).sizeDelta = new Vector2(Map.width, Map.height) * 8;
        }

        private void Start()
        {
            StartCoroutine(ApplyTexture());
        }

        public void Move(Vector2Int pos)
        {
            ((RectTransform)transform).DOAnchorPos(-pos * 8, .15f).SetEase(Ease.OutSine);
        }

        // ReSharper disable once IteratorNeverReturns
        private IEnumerator ApplyTexture()
        {
            while (true)
            {
                yield return new WaitForSeconds(0.5f);
                Map.Apply();
            }
        }

        public void UpdateTile(Vector2Int tilePos, TileBase tile)
        {
            var pos = tilePos + new Vector2Int(GameGenerator.Instance.width, GameGenerator.Instance.height);
            if (tile == GameGenerator.Instance.tileUndiscovered)
                Map.SetPixel(pos.x, pos.y, new Color32(0xC0, 0xC0, 0xC0, 0xFF));
            if (tile == GameGenerator.Instance.tileEmpty)
                Map.SetPixel(pos.x, pos.y, new Color32(0xA0, 0xA0, 0xA0, 0xFF));
            if (tile == GameGenerator.Instance.tile1)
                Map.SetPixel(pos.x, pos.y, new Color32(0x00, 0x00, 0xFF, 0xFF));
            if (tile == GameGenerator.Instance.tile2)
                Map.SetPixel(pos.x, pos.y, new Color32(0x00, 0x80, 0x00, 0xFF));
            if (tile == GameGenerator.Instance.tile3)
                Map.SetPixel(pos.x, pos.y, new Color32(0xBF, 0x00, 0x00, 0xFF));
            if (tile == GameGenerator.Instance.tile4)
                Map.SetPixel(pos.x, pos.y, new Color32(0x00, 0x00, 0x80, 0xFF));
            if (tile == GameGenerator.Instance.tile5)
                Map.SetPixel(pos.x, pos.y, new Color32(0x80, 0x00, 0x00, 0xFF));
            if (tile == GameGenerator.Instance.tile6)
                Map.SetPixel(pos.x, pos.y, new Color32(0x00, 0x80, 0x80, 0xFF));
            if (tile == GameGenerator.Instance.tile7)
                Map.SetPixel(pos.x, pos.y, new Color32(0x00, 0x00, 0x00, 0xFF));
            if (tile == GameGenerator.Instance.tile8)
                Map.SetPixel(pos.x, pos.y, new Color32(0x80, 0x80, 0x80, 0x8F));
            if (tile == GameGenerator.Instance.tileMark)
                Map.SetPixel(pos.x, pos.y, new Color32(0xFF, 0x00, 0x00, 0x8F));
            if (tile == GameGenerator.Instance.tileExploded)
                Map.SetPixel(pos.x, pos.y, new Color32(0x50, 0x00, 0x00, 0x8F));
        }
    }
}